import http from './request.js'

let winHeight = ""
let pixelRatio = ""
let screenHeight = ""
let allDictList = ""

let format = (fmt, date) => {
	let ret;
	const opt = {
		"Y+": date.getFullYear().toString(), // 年
		"m+": (date.getMonth() + 1).toString(), // 月
		"d+": date.getDate().toString(), // 日
		"H+": date.getHours().toString(), // 时
		"M+": date.getMinutes().toString(), // 分
		"S+": date.getSeconds().toString() // 秒
		// 有其他格式化字符需求可以继续添加，必须转化成字符串
	};
	for (let k in opt) {
		ret = new RegExp("(" + k + ")").exec(fmt);
		if (ret) {
			let target = ret[1].length==1?opt[k]:myPadStart(opt[k],ret[1].length);
			// fmt = fmt.replace(ret[1], (ret[1].length == 1) ? (opt[k]) : (opt[k].padStart(ret[1].length, "0")))
			fmt = fmt.replace(ret[1], (ret[1].length == 1) ? (opt[k]) : target);
		}
	};
	return fmt;
}
//替换padStart的方法
let myPadStart = (str,len)=>{
	let strLen = str.length
	for(let i =0;i<len-str.length;i++){
		str = '0'+str
	}
	return str
}
let dictCode = async (dict, code) => {
	let dictArr = await http.get({
		url: `/enrollment/sysDict/getItemList/${dict}`,
	})
	let res = ""
	dictArr.result.forEach(item => {
		if (item.dictValue == code) {
			res = item.dictName
		}
	})
	return res
}
let dectList = async (dict) => {
	let list = await http.get({
		url: `/enrollment/sysDict/getItemList/${dict}`,
	})
	return list.result
}
let getDictName = async(list,code)=>{
	let res=''
	list.forEach(item => {
		if (item.dictValue == code) {
			res = item.dictName
		}
	})
	return res
}
let translateDict=async (dict,code)=>{
	let res=''
	let list = await http.get({
		url: `/enrollment/sysDict/getItemList/${dict}`,
	})
	list.forEach(item => {
		if (item.dictValue == code) {
			res = item.dictName
		}
	})
	return res
}

uni.getSystemInfo({
	success(res) {
		winHeight = res.windowHeight + 'px'
		screenHeight = res.screenHeight 
		pixelRatio = res.pixelRatio
	}
})


export {
	format,
	winHeight,
	dictCode,
	pixelRatio,
	screenHeight,
	dectList,
	getDictName
}
